package com.fang.common.exception;

import com.fang.common.common.CommonResult;
import com.fang.common.util.CommonResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLSyntaxErrorException;

/**
 * @author Bernie_fang
 * @Description: 全局异常处理类
 * @create 2024/10/4 17:36
 **/
@Slf4j
@ControllerAdvice(basePackages = {"com.fang.networkdisk"})
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = NetworkDiskException.class)
    public CommonResult handle(NetworkDiskException e) {
        if (e.getErrorCode() != null) {
            return CommonResultUtil.failed(e);
        }
        log.error(this.getTrace(e));
        return CommonResultUtil.failed(ExceptionCode.CommonCode.SYSTEM_BUSY);
    }

//    @ResponseBody
//    @ExceptionHandler(value = MethodArgumentNotValidException.class)
//    public CommonResult handleValidException(MethodArgumentNotValidException e) {
//        // BindingResult bindingResult = e.getBindingResult();
//        // String message = null;
//        // if (bindingResult.hasErrors()) {
//        //     FieldError fieldError = bindingResult.getFieldError();
//        //     if (fieldError != null) {
//        //         message = fieldError.getField() + fieldError.getDefaultMessage();
//        //     }
//        // }
//        // return ResultUtil.validateFailed(message);
//        BindingResult bindingResult = e.getBindingResult();
//        String message = null;
//        if (bindingResult.hasErrors()) {
//            FieldError fieldError = bindingResult.getFieldError();
//            if (fieldError != null) {
//                message = fieldError.getField() + fieldError.getDefaultMessage();
//            }
//        }
//        log.error(message);
//        log.error(this.getTrace(e));
//        return CommonResultUtil.failed(ExceptionCode.CommonCode.ARGUMENT_NOT_VALID);
//    }

    // @ResponseBody
    // @ExceptionHandler(value = BindException.class)
    // public CommonResult handleValidException(BindException e) {
    //     BindingResult bindingResult = e.getBindingResult();
    //     String message = null;
    //     if (bindingResult.hasErrors()) {
    //         FieldError fieldError = bindingResult.getFieldError();
    //         if (fieldError != null) {
    //             message = fieldError.getField() + fieldError.getDefaultMessage();
    //         }
    //     }
    //     return ResultUtil.validateFailed(message);
    // }

    @ResponseBody
    @ExceptionHandler(value = SQLSyntaxErrorException.class)
    public CommonResult handleSQLSyntaxErrorException(SQLSyntaxErrorException e) {
        log.error(this.getTrace(e));
        return CommonResultUtil.failed(ExceptionCode.CommonCode.SQL_ERROR);
    }


    /**
     * Description: 拿到错误的栈堆信息，变成String
     * @Author: Bernie_fang
     * @Since: 2024/10/4 22:33
     * @param t:
     * @return: java.lang.String
     **/
    public String getTrace(Throwable t) {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        return buffer.toString();
    }


}
